/**
 * Copyright (c) 2013 CEIBA SOFTWARE HOUSE S.A.
 * <br><A HREF="http://www.ceiba.com.co>"</br>
 * Todos los derechos reservados.
 */

package co.com.smart.eagle.client.view.impl.smartcelltable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gwt.cell.client.AbstractInputCell;
import com.google.gwt.cell.client.SelectionCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

/**
 * Explicaci�n de la clase
 *
 * @version
 * @author jhonny.ocampo
 * <br><b>Fecha de desarrollo : </b> 3/10/2013
 * <br><b>Fecha de modificaci�n : </b> 3/10/2013
 *
 */

public class SmartSelectionEnabledCell extends AbstractInputCell<Seleccionable, String> {

	
	interface Template extends SafeHtmlTemplates {
	    @Template("<option value=\"{0}\">{1}</option>")
	    SafeHtml deselected(String option, String descripcion);

	    @Template("<option value=\"{0}\" selected=\"selected\">{1}</option>")
	    SafeHtml selected(String option, String descripcion);
	  }

	  private static Template template;

	  private HashMap<String, Integer> indexForOption = new HashMap<String, Integer>();

	  private final List<Seleccionable> options;

	  /**
	   * Construct a new {@link SelectionCell} with the specified options.
	   *
	   * @param options the options in the cell
	   */
	  public SmartSelectionEnabledCell(List<Seleccionable> options) {
	    super(BrowserEvents.CHANGE);
	    if (template == null) {
	      template = GWT.create(Template.class);
	    }
	    this.options = new ArrayList<Seleccionable>(options);
	    Seleccionable equi = new Seleccionable();
	    equi.setDescripcion("SELECCIONE");
	    equi.setId("-1");
	    this.options.add(0, equi);
	    int index = 0;
	    for (Seleccionable option : this.options) {
	      indexForOption.put(option.getId(), index++);
	    }
	  }

	  @Override
	  public void onBrowserEvent(Context context, Element parent, Seleccionable value,
	      NativeEvent event, ValueUpdater<Seleccionable> valueUpdater) {
	    super.onBrowserEvent(context, parent, value, event, valueUpdater);
	    String type = event.getType();
	    if (BrowserEvents.CHANGE.equals(type)) {
	      Object key = context.getKey();
	      SelectElement select = parent.getFirstChild().cast();
	      Seleccionable newValue = options.get(select.getSelectedIndex());
	      setViewData(key, newValue.getId());
	      finishEditing(parent, newValue, key, valueUpdater);
	      if (valueUpdater != null) {
	        valueUpdater.update(newValue);
	      }
	    }
	  }

	  @Override
	  public void render(Context context, Seleccionable value, SafeHtmlBuilder sb) {
	    // Get the view data.
	    Object key = context.getKey();
	    String viewData = getViewData(key);
	    if (viewData != null && viewData.equals(value)) {
	      clearViewData(key);
	      viewData = null;
	    }

	    int selectedIndex = getSelectedIndex(viewData == null ? (value != null ? (value.getId() != null ? value.getId() : "-1") : "-1") : viewData);
	    
	    if(value.isSeleccionable()){
	    	sb.appendHtmlConstant("<select tabindex=\"-1\">");
		    int index = 0;
		    for (Seleccionable option : options) {
		      if (index++ == selectedIndex) {
		        sb.append(template.selected(option.getId(), option.getDescripcion()));
		      } else {
		        sb.append(template.deselected(option.getId(), option.getDescripcion()));
		      }
		    }
		    sb.appendHtmlConstant("</select>");
	    }
	    else{
	    	sb.appendHtmlConstant("<div></div>");
	    }
	    
	  }

	  private int getSelectedIndex(String value) {
	    Integer index = indexForOption.get(value);
	    if (index == null) {
	      return -1;
	    }
	    return index.intValue();
	  }

}


/*
 * @author Administrador -- DD/MM/YYYY -- ID MANTIS -- BREVE DESCRIPCION
 */
